rm(list = ls())

library(data.table)
library(dplyr)
library(ggplot2)

data <- readRDS('./replication_hasz/output/data/data_pooled.rds')
data$study <- factor(data$study, levels=c('Pooled','Florida Study','U.S. Study'))

### plot outcome means by experimental condition and study
## vote_ownc
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(vote_ownc, na.rm = TRUE),
    sd=sd(vote_ownc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in own local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  xlab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/fig2b.pdf', width=6.5, height=5.5)


data_plot <- data[democrat==1 | republican==1]
my_sum <- data_plot[] %>%
  group_by(treatment, study, republican) %>%
  summarise( 
    n=n(),
    mean=mean(vote_ownc, na.rm = TRUE),
    sd=sd(vote_ownc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Democrat", "Republican")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=treatment, y=mean, fill=as.factor(republican))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_fill_grey(name=NULL, labels = dem.labs, start = 0.3, end = 0.6) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in own local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/fig3b.pdf', width=6.5, height=5.5)

### vote_usc
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(vote_usc, na.rm = TRUE),
    sd=sd(vote_usc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in U.S. local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  xlab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/fig2a.pdf', width=6.5, height=5.5)

data_plot <- data[democrat==1 | republican==1]
my_sum <- data_plot[] %>%
  group_by(treatment, study, republican) %>%
  summarise( 
    n=n(),
    mean=mean(vote_usc, na.rm = TRUE),
    sd=sd(vote_usc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Democrat", "Republican")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=treatment, y=mean, fill=as.factor(republican))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_fill_grey(name=NULL, labels = dem.labs, start = 0.3, end = 0.6) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in U.S. local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  xlab(NULL) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/fig3a.pdf', width=6.5, height=5.5)


### ballot
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(ballotc, na.rm = TRUE),
    sd=sd(ballotc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))


ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in STATE local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  xlab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/fig2c.pdf', width=6.5, height=5.5)

data_plot <- data[democrat==1 | republican==1]
my_sum <- data_plot[] %>%
  group_by(treatment, study, republican) %>%
  summarise( 
    n=n(),
    mean=mean(ballotc, na.rm = TRUE),
    sd=sd(ballotc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Democrat", "Republican")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=treatment, y=mean, fill=as.factor(republican))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_fill_grey(name=NULL, labels = dem.labs, start = 0.3, end = 0.6) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in STATE local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  theme_bw(base_size = 12) + theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/fig3c.pdf', width=6.5, height=5.5)


### more_info
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(more_infoc, na.rm = TRUE),
    sd=sd(more_infoc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))


ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  scale_y_continuous(name = 'Willingness to learn about noncitizen voting', breaks = c(0,1),
                     labels = c('No', 'Yes'),
                     limits = c(0,1)) +
  ylab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/fig2d.pdf', width=6.5, height=5.5)

data_plot <- data[democrat==1 | republican==1]
my_sum <- data_plot[] %>%
  group_by(treatment, study, republican) %>%
  summarise( 
    n=n(),
    mean=mean(more_infoc, na.rm = TRUE),
    sd=sd(more_infoc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

dem.labs <- c("Democrat", "Republican")
names(dem.labs) <- c("0", "1")

ggplot(my_sum, aes(x=treatment, y=mean, fill=as.factor(republican))) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_fill_grey(name=NULL, labels = dem.labs, start = 0.3, end = 0.6) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  scale_y_continuous(name = 'Willingness to learn about noncitizen voting', breaks = c(0,1),
                     labels = c('No', 'Yes'),
                     limits = c(0,1)) +
  ylab(NULL) +
  theme_bw(base_size = 11) + theme(plot.title = element_text(size = 10), axis.text.x=element_text(size=10), legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/fig3d.pdf', width=6.5, height=5.5)